const {ccclass, property} = cc._decorator;


interface folds{
    [ley: string]: cc.Sprite[];
}


/**
 * 更新显示隐藏牌的脚本
 */
@ccclass
export default class Folds extends cc.Component {

    /**四个玩家的手牌原始信息(就是建场景的时候放的那些模板图片) */
    folds: folds = null;

    onLoad() {
        if(!cc.vv){
            return;
        }

        this.initView();
        this.initEventHandlers();
        this.initAllFolds();
    }

    initView(){
        this.folds = {};
        const game = this.node.getChildByName('game');
        for(let key in cc.vv.directions){
            const sideName = cc.vv.directions[key];
            const sideRoot = game.getChildByName(sideName);
            const folds = [];
            const foldRoot = sideRoot.getChildByName('folds');
            foldRoot.children.forEach((item, index) => {
                item.active = false;
                let sprite = item.getComponent(cc.Sprite);
                sprite.spriteFrame = null;
                folds.push(sprite);
            });
            this.folds[sideName] = folds;
        }

        this.hideAllFolds();
    }

    /**隐藏所有的手牌 */
    hideAllFolds(){
        for(let k in this.folds){
            let f = this.folds[k];
            f.forEach((item, index) => {
                item.node.active = false;
            });
        }
    }


    /**初始化需要监听的消息 */
    initEventHandlers(){

        this.node.on(cc.vv.game_event_map.GAME_BEGIN, (data) => {
            this.initAllFolds();
        });

        this.node.on(cc.vv.game_event_map.GAME_SYNC, (data) => {
            this.initAllFolds();
        });

        this.node.on(cc.vv.game_event_map.GAME_CHUPAI_NOTIFY, (data) => {
            this.initFolds(data.detail);
        });

        this.node.on(cc.vv.game_event_map.GUO_NOTIFY, (data) => {
            this.initFolds(data.detail);
        });
    }


    /**更新四个玩家的手牌 */
    initAllFolds(){
        const seats = cc.vv.gameNetMgr.seats;
        seats.forEach((seat) => {
            this.initFolds(seat);
        });
    }


    /**更新某个方位的手牌 */
    initFolds(seatData: cc.seat){
        const folds = seatData.folds;
        if(folds == null){
            return;
        }
        const localIndex = cc.vv.gameNetMgr.getLocalIndex(seatData.seatIndex);
        const pre = cc.vv.mahjongmgr.getFoldPre(localIndex);
        const side = cc.vv.mahjongmgr.getSide(localIndex);

        const foldsSprites = this.folds[side];
        // 显示系统发的牌
        for(let i = 0; i < foldsSprites.length; i++){
            let index = i;
            if(side == cc.vv.directions.right || side == cc.vv.directions.up){
                index = foldsSprites.length - i - 1;
            }
            const sprite = foldsSprites[index];
            sprite.node.active = true;
            this.setSpriteFrameByMJID(pre, sprite, folds[i]);
        }
        // 隐藏已经碰杠等区域原始位置的牌
        for(let i = folds.length; i < foldsSprites.length; i++){
            let index = i;
            if(side == cc.vv.directions.right || side == cc.vv.directions.up){
                index = foldsSprites.length - i - 1;
            }
            const sprite = foldsSprites[index];
            sprite.spriteFrame = null;
            sprite.node.active = false;
        }
    }

    /**
     * 显示牌
     * @param pre 前缀字符串
     * @param sprite 那个位置的图片
     * @param mjid id
     */
    setSpriteFrameByMJID(pre: string, sprite: cc.Sprite, mjid: number){
        sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, mjid);
        sprite.node.active = true;
    }
}
